local sk__lianti = fk.CreateSkill {

  name = "sk__lianti",

  tags = { Skill.Compulsory, },

}

sk__lianti:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  if not player.chained then
    player:setChainState(true)
  end
end)

sk__lianti:addEffect(fk.BeforeChainStateChange, {
  name = "sk__lianti",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__lianti.name) then
      return target == player and player.chained
    end
  end,
  on_use = function(self, event, target, player, data)
    data.prevented = true
  end,
})
sk__lianti:addEffect(fk.Damaged, {
  name = "sk__lianti",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__lianti.name) then
      if target ~= player and player.phase ~= Player.NotActive and data.damageType ~= fk.NormalDamage and not target.dead then
        local room = player.room
        local damage_event = room.logic:getCurrentEvent()
        if not damage_event then return end
        local x = 0
        room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function(e)
          local reason = e.data.reason
          if reason == "damage" then
            local first_damage_event = e:findParent(GameEvent.Damage)
            if first_damage_event then
              local damage = first_damage_event.data
              if damage.damageType ~= fk.NormalDamage and damage.to ~= player then
                x = first_damage_event.id
                return true
              end
            end
          end
        end, Player.HistoryTurn)
        return damage_event.id == x
      else
        return target == player and data.damageType ~= fk.NormalDamage
      end
    end
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      player:broadcastSkillInvoke(sk__lianti.name)
      if target ~= player then
        room:notifySkillInvoked(player, sk__lianti.name, "offensive")
        room:doIndicate(player.id, { target.id })
        room:damage {
          from = player,
          to = target,
          damage = data.damage,
          damageType = data.damageType,
          skillName = sk__lianti.name,
        }
      else
        room:notifySkillInvoked(player, sk__lianti.name, "drawcard")
        room:addPlayerMark(player, MarkEnum.AddMaxCards, 1)
        room:addPlayerMark(player, MarkEnum.SkDrawNCards, 1)
        local damage_event = room.logic:getCurrentEvent():findParent(GameEvent.Damage)
        if not damage_event then return end
        local x = 0
        room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function(e)
          local reason = e.data.reason
          if reason == "damage" then
            local first_damage_event = e:findParent(GameEvent.Damage)
            if first_damage_event then
              local damage = first_damage_event.data
              if damage.damageType ~= fk.NormalDamage and damage.to == player then
                x = first_damage_event.id
                return true
              end
            end
          end
        end, Player.HistoryRound)
        if damage_event.id == x then
          room:changeMaxHp(player, -1)
        end
      end
  end,
})

return sk__lianti
